package mysqldao

import (
	"ahutoj/web/dao"
	"context"
	"gorm.io/gorm"
)

func SelectTraningByLID(ctx context.Context, LID int64) (dao.List, error) {
	db := GetDB(ctx)
	ret := dao.List{}
	err := db.Table("List").Where("LID=?", LID).Find(&ret).Error
	return ret, err
}

func SelectTraningList(ctx context.Context, offset, limit int64) ([]dao.List, error) {
	db := GetDB(ctx)
	ret := make([]dao.List, 0)
	err := db.Table("List").Offset(int(offset)).Limit(int(limit)).Find(&ret).Error
	return ret, err
}

func InsertTraning(ctx context.Context, traning dao.List) error {
	db := GetDB(ctx)
	return db.Table("List").Create(&traning).Error
}

func DeleteTraning(ctx context.Context, LID int64) error {
	db := GetDB(ctx)
	return db.Table("List").Where("LID=?",LID).Delete(&dao.List{}).Error
}

func UpdateTraning(ctx context.Context, traning dao.List) error {
	db := GetDB(ctx)
	return db.Table("List").Where("LID=?", traning.LID).Updates(traning).Error
}
func SaveTraning(ctx context.Context, traning dao.List) error {
	db := GetDB(ctx)
	return db.Table("List").Where("LID=?", traning.LID).Save(traning).Error
}
func SelectListCountByLID(ctx context.Context, LID int64) (count int64, err error) {
	db := GetDB(ctx)
	err = db.Table("List").Where("LID=?", LID).Count(&count).Error
	return count, err
}
func InsertListProblem(ctx context.Context, training dao.ListProblem) error {
	db := GetDB(ctx)
	return db.Table("ListProblem").Create(&training).Error
}
func IsExistListProblem(ctx context.Context, LID int64, PID string) (bool, error) {
	db := GetDB(ctx)
	var cnt int64
	err := db.Table("ListProblem").Where("LID=? and PID=?", LID, PID).Count(&cnt).Error
	if err != nil {
		return false, err
	}
	return cnt > 0, nil
}
func UpdateListProblem(ctx context.Context, training dao.ListProblem) error {
	db := GetDB(ctx)
	return db.Table("ListProblem").Where("LID=?", training.LID).Updates(training).Error
}
func GetTrainingList(ctx context.Context, offset, limit int) ([]dao.List, error) {
	db := GetDB(ctx)
	tp := dao.List{}
	ret := make([]dao.List, 0)
	err := db.Table(tp.TableName()).Order("StartTime desc").Offset(offset).Limit(limit).Find(&ret).Error
	return ret, err
}

func SelectListByUID(ctx context.Context, UID string) (int64, error) {
	db := GetDB(ctx)
	ret := dao.List{}
	err := db.Where("UID=?", UID).Last(&ret).Error
	return ret.LID, err
}
func SelectTraningListCount(ctx context.Context) (int64, error) {
	db := GetDB(ctx)
	var ret int64
	err := db.Count(&ret).Error
	return ret, err
}
func GetTraining(ctx context.Context, LID int64) (*dao.List, error) {
	db := GetDB(ctx)
	ret := new(dao.List)
	err := db.Table(ret.TableName()).Where("LID=?", LID).Find(ret).Error
	return ret, err
}

func SelectTrainProblemByLID(ctx context.Context, LID int64) ([]dao.ListProblem, error) {
	db := GetDB(ctx)
	ret := make([]dao.ListProblem, 0)
	err := db.Table(dao.ListProblem{}.TableName()).Where("LID=?", LID).Find(&ret).Error
	return ret, err
}

func SelectListCountByList(ctx context.Context, list dao.List) (ret int64,err error) {
	db := GetDB(ctx)
	err = db.Table(list.TableName()).Where(list).Count(&ret).Error
	return ret, err
}

// func IncListSolved(ctx context.Context, LID int64, PID string) error {
// 	db := GetDB(ctx)
// 	listpro := dao.ListProblem{}
// 	return db.Table(listpro.TableName()).Where("LID=? and PID=?", LID, PID).UpdateColumn("Solved", gorm.Expr("Solved+1")).Error
// }
func IncListUserSubmit(ctx context.Context, LID int64, UID string) error {
	db := GetDB(ctx)
	listuser := dao.ListUser{}
	return db.Table(listuser.TableName()).Where("LID=? and UID=?", LID, UID).UpdateColumn("Submited", gorm.Expr("Submited+1")).Error
}
func IncListUserSolved(ctx context.Context, LID int64, UID string) error {
	db := GetDB(ctx)
	listuser := dao.ListUser{}
	return db.Table(listuser.TableName()).Where("LID=? and UID=?", LID, UID).UpdateColumn("Solved", gorm.Expr("Solved+1")).Error
}//用不上了，因为学长的Solve是直接从submit表里面拿的(用来显示已经写过的题/ahutoj/web/logic/training.go GetTrainUserInfo)

func UpdateListUserSolved(ctx context.Context, LID int64, solved int)error{
	db:=GetDB(ctx)
	listuser:=dao.ListUser{}
	return db.Table(listuser.TableName()).Where("LID=?", LID).UpdateColumn("Solved", solved).Error
}